﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using EncryptionLib.Users;
using EncryptionLib;
using EncryptionLib.Util;
using System.Security.Cryptography;
using EncryptionLib.Encryption;
using EncryptionLib.Signature;


namespace TotalCommander_v3
{
    public partial class frmUserSetting : Form
    {
             // use e temporary file to save.
        //public IUserManager m_userManager = null;
        public string m_sEmail = String.Empty;

        public frmUserSetting()
        {
            InitializeComponent();
        }

        private void frmUserSetting_Load(object sender, EventArgs e)
        {
            try
            {
                UserData user = frmMain.m_userManager.GetUserByEmail(m_sEmail);
                tbAddress.Text = user.Address;
                tbEmail.Text = user.Email;
                tbName.Text = user.Name;
                tbPhone.Text = user.PhoneNumber;

                //
                cbkChangePassphrase.Checked = false;
                cbkShowPassphrase.Enabled = false;
                tbPassphrase.Enabled = false;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("You have to login first.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
            }
        }

        private void cbkShowPassphrase_CheckedChanged(object sender, EventArgs e)
        {
            tbPassphrase.PasswordChar = cbkShowPassphrase.Checked ? '\0' : '*';
        }

        private void cbkChangePassphrase_CheckedChanged(object sender, EventArgs e)
        {
            if (cbkChangePassphrase.Checked)
            {
                cbkShowPassphrase.Enabled = true;
                tbPassphrase.Enabled = true;
            }
            else
            {
                cbkShowPassphrase.Enabled = false;
                tbPassphrase.Enabled = false;
            }
        }

        private void btChange_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tbName.Text) || String.IsNullOrEmpty(dtBirthday.Value.ToString()) 
                || String.IsNullOrEmpty(tbAddress.Text)|| String.IsNullOrEmpty(tbPhone.Text))
            {
                MessageBox.Show("Yout must supply all information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (cbkChangePassphrase.Checked && String.IsNullOrEmpty(tbPassphrase.Text))
            {
                MessageBox.Show("New passphrase can not be null or empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                UserData user = frmMain.m_userManager.GetUserByEmail(m_sEmail);

                user.Name = tbName.Text;
                user.PhoneNumber = tbPhone.Text;
                user.Address = tbAddress.Text;
                user.Birthday = dtBirthday.Value;

                if (cbkChangePassphrase.Checked)
                {
                    user.Passphrase = new Passphrase(SerializerHelper.String2Bytes(tbPassphrase.Text));
                }

                frmMain.m_userManager.UpdateUserData(user);
                frmMain.m_userManager.SaveData();

                MessageBox.Show("Change successful!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e1)
            {
                MessageBox.Show("Change Failed: " + e1.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      

        
    }
}
